home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / movgen.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  53KB  |  1,292 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* modified 03/11/97 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   GenerateCaptures() is used to generate capture and pawn promotion moves    *
  11. *   from the current position.                                                 *
  12. *                                                                              *
  13. *   the destination square set is the set of squares occupied by opponent      *
  14. *   pieces, plus the set of squares on the 8th rank that pawns can advance to  *
  15. *   and promote.                                                               *
  16. *                                                                              *
  17. ********************************************************************************
  18. */
  19. int* GenerateCaptures(TREE *tree, int ply, int wtm, int *move)
  20. {
  21.   register BITBOARD target, piecebd, moves;
  22.   register BITBOARD  promotions, pcapturesl, pcapturesr;
  23.   register int from, to, temp;
  24.  
  25.   if (wtm) {
  26. /*
  27.  ----------------------------------------------------------
  28. |                                                          |
  29. |   now, produce knight moves by cycling through the       |
  30. |   *_knight board to locate a [from] square and then      |
  31. |   cycling through knight_attacks[] to locate to squares  |
  32. |   that a knight on [from] attacks.                       |
  33. |                                                          |
  34.  ----------------------------------------------------------
  35. */
  36.     piecebd=WhiteKnights;
  37.     while (piecebd) {
  38.       from=LastOne(piecebd);
  39.       moves=And(knight_attacks[from],BlackPieces);
  40.       temp=from+(knight<<12);
  41.       while (moves) {
  42.         to=LastOne(moves);
  43.         *move++=temp|(to<<6)|((-PieceOnSquare(to))<<15);
  44.         Clear(to,moves);
  45.       }
  46.       Clear(from,piecebd);
  47.     }
  48. /*
  49.  ----------------------------------------------------------
  50. |                                                          |
  51. |   now, produce bishop moves by cycling through the       |
  52. |   *_bishop board to locate a [from] square and then      |
  53. |   generate the AttacksFrom() bitmap which supplies the   |
  54. |   list of valid <to> squares.                            |
  55. |                                                          |
  56.  ----------------------------------------------------------
  57. */
  58.     piecebd=WhiteBishops;
  59.     while (piecebd) {
  60.       from=LastOne(piecebd);
  61.       moves=And(AttacksBishop(from),BlackPieces);
  62.       temp=from+(bishop<<12);
  63.       while (moves) {
  64.         to=LastOne(moves);
  65.         *move++=temp|(to<<6)|((-PieceOnSquare(to))<<15);
  66.         Clear(to,moves);
  67.       }
  68.       Clear(from,piecebd);
  69.     }
  70. /*
  71.  ----------------------------------------------------------
  72. |                                                          |
  73. |   now, produce rook moves by cycling through the         |
  74. |   *_rook board to locate a [from] square and then        |
  75. |   generate the AttacksFrom() bitmap which supplies the   |
  76. |   list of valid <to> squares.                            |
  77. |                                                          |
  78.  ----------------------------------------------------------
  79. */
  80.     piecebd=WhiteRooks;
  81.     while (piecebd) {
  82.       from=LastOne(piecebd);
  83.       moves=And(AttacksRook(from),BlackPieces);
  84.       temp=from+(rook<<12);
  85.       while (moves) {
  86.         to=LastOne(moves);
  87.         *move++=temp|(to<<6)|((-PieceOnSquare(to))<<15);
  88.         Clear(to,moves);
  89.       }
  90.       Clear(from,piecebd);
  91.     }
  92. /*
  93.  ----------------------------------------------------------
  94. |                                                          |
  95. |   now, produce queen moves by cycling through the        |
  96. |   *_queen board to locate a [from] square and then       |
  97. |   generate the AttacksFrom() bitmap which supplies the   |
  98. |   list of valid <to> squares.                            |
  99. |                                                          |
  100.  ----------------------------------------------------------
  101. */
  102.     piecebd=WhiteQueens;
  103.     while (piecebd) {
  104.       from=LastOne(piecebd);
  105.       moves=And(AttacksQueen(from),BlackPieces);
  106.       temp=from+(queen<<12);
  107.       while (moves) {
  108.         to=LastOne(moves);
  109.         *move++=temp|(to<<6)|((-PieceOnSquare(to))<<15);
  110.         Clear(to,moves);
  111.       }
  112.       Clear(from,piecebd);
  113.     }
  114. /*
  115.  ----------------------------------------------------------
  116. |                                                          |
  117. |   now, produce king moves by cycling through the         |
  118. |   *_king board to locate a [from] square and then        |
  119. |   cycling through king_attacks[] to locate to squares    |
  120. |   that a king on [from] attacks.                         |
  121. |                                                          |
  122.  ----------------------------------------------------------
  123. */
  124.     from=WhiteKingSQ;
  125.     moves=And(king_attacks[from],BlackPieces);
  126.     temp=from+(king<<12);
  127.     while (moves) {
  128.       to=LastOne(moves);
  129.       *move++=temp|(to<<6)|((-PieceOnSquare(to))<<15);
  130.       Clear(to,moves);
  131.     }
  132. /*
  133.  ----------------------------------------------------------
  134. |                                                          |
  135. |   now, produce pawn moves.  this is done differently due |
  136. |   to inconsistencies in the way a pawn moves when it     |
  137. |   captures as opposed to normal non-capturing moves.     |
  138. |   another exception is capturing enpassant.  the first   |
  139. |   step is to generate all possible pawn promotions.  we  |
  140. |   do this by masking  all pawns but those on the 7th     |
  141. |   rank and then advancing them ahead if the square in    |
  142. |   front is empty.                                        |
  143. |                                                          |
  144.  ----------------------------------------------------------
  145. */
  146.     promotions=And(Shiftr(And(WhitePawns,rank_mask[RANK7]),8),Compl(Occupied));
  147.     while (promotions) {
  148.       to=LastOne(promotions);
  149.       *move++=(to-8)|(to<<6)|(pawn<<12)|(queen<<18);
  150.       Clear(to,promotions);
  151.     }
  152.  
  153.     target=Or(BlackPieces,EnPassantTarget(ply));
  154.     pcapturesl=And(Shiftr(And(WhitePawns,mask_left_edge),7),target);
  155.     while (pcapturesl) {
  156.       to=LastOne(pcapturesl);
  157.       if (to < 56) {
  158.         if(PieceOnSquare(to)) 
  159.           *move++=(to-7)|(to<<6)|(pawn<<12)|((-PieceOnSquare(to))<<15);
  160.         else
  161.           *move++=(to-7)|(to<<6)|(pawn<<12)|(pawn<<15);
  162.       }
  163.       else 
  164.         *move++=(to-7)|(to<<6)|(pawn<<12)|((-PieceOnSquare(to))<<15)|(queen<<18);
  165.       Clear(to,pcapturesl);
  166.     }
  167.  
  168.     pcapturesr=And(Shiftr(And(WhitePawns,mask_right_edge),9),target);
  169.     while (pcapturesr) {
  170.       to=LastOne(pcapturesr);
  171.       if (to < 56) {
  172.         if(PieceOnSquare(to)) 
  173.           *move++=(to-9)|(to<<6)|(pawn<<12)|(-PieceOnSquare(to)<<15);
  174.         else
  175.           *move++=(to-9)|(to<<6)|(pawn<<12)|(pawn<<15);
  176.       }
  177.       else 
  178.         *move++=(to-9)|(to<<6)|(pawn<<12)|((-PieceOnSquare(to))<<15)|(queen<<18);
  179.       Clear(to,pcapturesr);
  180.     }
  181.   }
  182. /*
  183.  ----------------------------------------------------------
  184. |                                                          |
  185. |   now, produce knight moves by cycling through the       |
  186. |   *_knight board to locate a [from] square and then      |
  187. |   cycling through knight_attacks[] to locate to squares  |
  188. |   that a knight on [from] attacks.                       |
  189. |                                                          |
  190.  ----------------------------------------------------------
  191. */
  192.   else {
  193.     piecebd=BlackKnights;
  194.     while (piecebd) {
  195.       from=FirstOne(piecebd);
  196.       moves=And(knight_attacks[from],WhitePieces);
  197.       temp=from+(knight<<12);
  198.       while (moves) {
  199.         to=FirstOne(moves);
  200.         *move++=temp|(to<<6)|(PieceOnSquare(to)<<15);
  201.         Clear(to,moves);
  202.       }
  203.       Clear(from,piecebd);
  204.     }
  205. /*
  206.  ----------------------------------------------------------
  207. |                                                          |
  208. |   now, produce bishop moves by cycling through the       |
  209. |   *_bishop board to locate a [from] square and then      |
  210. |   generate the AttacksFrom() bitmap which supplies the   |
  211. |   list of valid